;
define([ 'suredyModal' ], function(Modal) {
	// 新建资金
	$('body').on('click', '.table-edit-btn.new', function(event) {
		var $this = $(this);
		// boolean payOut, MoneyType type, String contractId, String source,
		// Model model
		var payOut = $this.hasClass('payout');
		var type = $this.data('type');
		var contractId = $this.data('contract-id');
		var source = $this.data('source');

		Modal.showModal({
			icon : 'icon-upload',
			title : '新建资金信息',
			showFoot : false,
			uri : Suredy.contextPath + '/money/new-form',
			data : {
				payOut : payOut,
				type : type,
				contractId : contractId,
				source : source
			}
		});
	});

	// 回收、退回资金
	$('body').on('click', '.table-edit-btn.change', function(event) {
		var $this = $(this);
		var moneyId = $this.data('money-id');

		Modal.showModal({
			size : 'normal',// lg sm normal
			showHead : true,
			showFoot : true,
			showBody : true,
			showOkBtn : true,
			showCancelBtn : true,
			ignoreBackgroundClick : true,
			backdrop : true,
			keyboard : true,
			icon : 'icon-exclamation-sign',
			title : '确认操作',
			okText : '确认',
			cancelText : '取消',
			text : '<div class="row" style="color: #c9302c;">\
	<div class="col-xs-12 text-center">\
		<h2>\
			<i class="icon-warning-sign" style="color: #f0ad4e; font-size: 40px;"></i>&nbsp;&nbsp;\
			是否确认此次操作？\
		</h2>\
	</div>\
	<div class="col-xs-12">\
		<hr>\
	</div>\
	<div class="col-xs-12">\
		<input type="checkbox" value="true" id="as-business-money">\
		<label for="as-business-money" style="font-weight: inherit; padding-left: 3px;">同时作为合同资金记录到合同资金账目</label>\
	</div>\
	<div class="col-xs-12">\
		<small style="color: #999;">\
			说明：勾选后，该资金会作为收（付）款合同的已收（付）款项进行记录\
		</small>\
	</div>\
</div>',
			okClick : function() {
				var asBusinessMoney = $('.modal input#as-business-money').prop('checked');

				$.ajax({
					url : Suredy.contextPath + '/money/back',
					data : {
						moneyId : moneyId,
						asBusinessMoney : asBusinessMoney
					},
					type : 'post',
					dataType : 'json',
					success : function(result, textStatus, jqXHR) {
						if (!result || !result.success) {
							var msg = (result && result.msg) || '操作失败！';
							alert(msg);
							return false;
						}

						Modal.closeModal();
						refresh();
					},
					error : function(jqXHR, textStatus, errorThrown) {
						alert('操作失败！\n\n' + errorThrown);
					}

				});
			}
		});
	});

	// 删除资金
	$('body').on('click', '.table-edit-btn.delete', function(event) {
		var $this = $(this);
		var moneyId = $this.data('money-id');

		if (!confirm('确认执行此操作？')) {
			return false;
		}

		$.ajax({
			url : Suredy.contextPath + '/money/delete',
			data : {
				moneyId : moneyId
			},
			type : 'post',
			dataType : 'json',
			success : function(result, textStatus, jqXHR) {
				if (!result || !result.success) {
					var msg = (result && result.msg) || '操作失败！';
					alert(msg);
					return false;
				}

				refresh();
			},
			error : function(jqXHR, textStatus, errorThrown) {
				alert('操作失败！\n\n' + errorThrown);
			}

		});
	});

});
